package com.cnwidsom.monitor;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.cnwidsom.config.MonitorConfig;
import com.cnwidsom.filter.Filter.BreakException;
import com.cnwidsom.filter.FilterChain;
import com.cnwidsom.filter.FilterUtils;
import com.cnwidsom.plugins.PluginUtils;
import com.cnwidsom.plugins.persistence.Persistence;

public class MonitorLogWritter {
	private static Queue<MonitorItem> queue = new ConcurrentLinkedQueue<MonitorItem>();

	private static Thread writter_process = new Thread(new Runnable() {

		public void run() {
			Persistence persistence = PluginUtils.getPersistenceByName(MonitorConfig.getCur_mc().getPersistenceType());

			while (true) {
				try {
					MonitorItem mi = queue.poll();
					FilterChain chain = MonitorConfig.getCur_mc().getFilterChains().get(FilterUtils.FILTER_TYPE_LOG);
					try {
						if (chain != null)
							chain.doChain(mi);
						if (mi != null) {
							persistence.writeData(mi);
						}
					} catch (BreakException ex) {
						// 跳过拦截器阻止的数据
					}
					if (queue.isEmpty()) {
						Thread.sleep(MonitorConfig.getCur_mc().getLogInterval());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	});

	public static void log(List datas) {
		if (!writter_process.isAlive()) {
			writter_process.start();
		}

		boolean needRecord = false;
		for (Object data : datas) {
			// JsonConfig jsonConfig = new JsonConfig();
			// jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			// JSONObject json = JSONObject.fromObject(data, jsonConfig);
			// System.out.println(json.toString());
			if (data instanceof Token) {
				Token token = (Token) data;
				// if (token.isNotRecord()) {
				// // 如果不记录此token,则从链表中去掉token纪录
				// if (token.getPreviousToken() != null && token.getNextToken()
				// != null) {
				// token.getPreviousToken().setNextToken(token.getNextToken());
				// token.getNextToken().setPreviousToken(token.getPreviousToken());
				//
				// if (token.getPreviousToken().getExtraData() instanceof
				// MonitorItem) {
				// ((MonitorItem)
				// token.getPreviousToken().getExtraData()).setNextToken(token.getNextToken().getCurTokenValue());
				// }
				// if (token.getNextToken().getExtraData() instanceof
				// MonitorItem) {
				// ((MonitorItem)
				// token.getNextToken().getExtraData()).setPreviousToken(token.getPreviousToken().getCurTokenValue());
				// }
				//
				// }
				// }
				if (token.getExtraData() instanceof MonitorItem) {
					MonitorItem mi = (MonitorItem) token.getExtraData();
					if (mi.isNeedRecord() && !mi.isNotRecord()) {
						needRecord = true;
						break;
					}
				}
			}
		}
		if (needRecord) {
			for (Object data : datas) {
				if (data instanceof Token) {
					if (((Token) data).getExtraData() instanceof MonitorItem) {
						MonitorItem mi = (MonitorItem) ((Token) data).getExtraData();
						// System.out.println(mi.toJsonString());
						if (!mi.isNotRecord()) {
							queue.add(mi);
						}
					}
				}
			}
		}
	}
}
